package com.mediacloud.app.newsmodule.fragment.recommend;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.CMSFieldStyle;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.ShowSwitch;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.NewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2022/12/27.
 * email：guangmingfei@chinamcloud.com
 * description：
 */
public class ComponentPopularRecommend extends BaseComponentPopularRecommendView implements AdapterView.OnItemClickListener {


    private ListView articleListView = null;
    private NewsListItemStyleAdaptor articlesAdapter = null;
    private CMSFieldStyle cmsFieldStyle = new CMSFieldStyle();
    private AppFactoryGlobalConfig.ServerAppConfigInfo serverAppConfigInfo;
    public ComponentPopularRecommend(Context context) {
        super(context);
        serverAppConfigInfo = AppFactoryGlobalConfig.getAppServerConfigInfo(context);
    }


    @Override
    public int setLayoutRes() {
        return R.layout.component_popular_recommend;
    }

    @Override
    public void initOtherViews() {
        articleListView = Utility.findViewById(mRootView, R.id.articleListView);
    }


    @Override
    public void onDataSuccess(ArrayList<ArticleItem> listArticles) {
        if (listArticles == null || listArticles.size() == 0) {
            return;
        }
        for (ArticleItem item : listArticles) {
            item.setCmsCustomStyle(cmsFieldStyle);//去掉cms样式
            // item.mShowSwitch = showSwitch;
            item.mShowSwitch = new ShowSwitch();
            item.mShowSwitch.allowShowHitCount = false;
            item.mShowSwitch.allowShowInteractionCount = false;
            item.mShowSwitch.allowShowPublishDate = true;
            item.mShowSwitch.allowShowSource = false;
            item.mShowSwitch.allowShowComment = false;

            item.setPublishDate_format(item.publishDate);
            item.setPublishdate(item.publishDate);
        }


        mRootView.setVisibility(VISIBLE);
        this.setVisibility(VISIBLE);
        //mRootView.findViewById(R.id.footerMoreTv).setVisibility(INVISIBLE);
        final CatalogItem cat = new CatalogItem();
        articlesAdapter = new NewsListItemStyleAdaptor(getContext(), cat);
        articlesAdapter.isEssenceList = true;
        articlesAdapter.isNewsDetailRecommend = true;
        articlesAdapter.isRecommendList = true;
        articlesAdapter.setListContentData(listArticles);
        if (articleListView == null)
            articleListView = Utility.findViewById(mRootView, R.id.articleListView);

        articleListView.setDivider(null);
        articleListView.setAdapter(articlesAdapter);
        articlesAdapter.notifyDataSetChanged();
        articleListView.setOnItemClickListener(ComponentPopularRecommend.this);
    }

    @Override
    public void onDataFault(Object data) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ArticleItem article = articlesAdapter.getItem(position);
        NewsItemClickUtils.OpenItemNewsHandle(getContext(), article.getType(), article, articlesAdapter.catalogItem);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (articlesAdapter != null) {
            articlesAdapter.destroy();
        }
    }
}